<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/3/13 19:26
 * @version
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class MobileRechargeTask extends Model
{
    protected $table = 'mobile_recharge_task';

    public $guarded = [];

    public $appends = ['status_name'];

    public function detail()
    {
        return $this->belongsTo(MobileRechargeOrderDetail::class, 'recharge_order_detail_id', 'id');
    }

    public function order()
    {
        return $this->belongsTo(MobileRechargeOrder::class, 'recharge_order_id', 'id');
    }

    public static function createTaskByOrder(RechargeOrder $order)
    {
        if ($order->type == MobileRechargeOrder::TYPE_RECHARGE) {
            foreach ($order->detail as $item) {
                MobileRechargeTask::createTask($order->mobile, $item->amount, $item->pay_at, $order->id, $item->id);
            }
        } else {
            MobileRechargeTask::createTask($order->mobile, $order->recharge_amount, $order->paid_at, $order->id);
        }
    }

    public static function createTask($mobile, $amount, $payAt, $orderId, $detailId = 0)
    {
        // 创建充值任务
        $task = [
            'recharge_order_id'        => $orderId,
            'recharge_order_detail_id' => $detailId,
            'sn'                       => str_random(32),
            'mobile'                   => $mobile,
            'pay_at'                   => $payAt,
            'status'                   => 0,
            'result'                   => ''
        ];
        $times_500 = floor($amount / 500);
        $plus_500  = $amount % 500;
        $times_300 = floor($plus_500 / 300);
        $plus_300  = $plus_500 % 300;
        $times_200 = floor($plus_300 / 200);
        $plus_200  = $plus_300 % 200;
        $times_100 = floor($plus_200 / 100);
        $plus_100  = $plus_200 % 100;
        $times_50  = floor($plus_100 / 50);
        $plus_50   = $plus_100 % 50;
        if($times_500 > 0){
            for($i=0;$i<$times_500;$i++){
                $task['amount'] = 500;
                $task['sn'] = date('YmdHis') . Str::random(16);
                MobileRechargeTask::create($task);
            }
        }

        if($times_300 > 0){
            for($i=0;$i<$times_300;$i++){
                $task['amount'] = 300;
                $task['sn'] = date('YmdHis') . Str::random(16);
                MobileRechargeTask::create($task);
            }
        }

        if($times_200 > 0){
            for($i=0;$i<$times_200;$i++){
                $task['amount'] = 200;
                $task['sn'] = date('YmdHis') . Str::random(16);
                MobileRechargeTask::create($task);
            }
        }

        if($times_100 > 0){
            for($i=0;$i<$times_100;$i++){
                $task['amount'] = 100;
                $task['sn'] = date('YmdHis') . Str::random(16);
                MobileRechargeTask::create($task);
            }
        }

        if($times_50 > 0){
            for($i=0;$i<$times_50;$i++){
                $task['amount'] = 50;
                $task['sn'] = date('YmdHis') . Str::random(16);
                MobileRechargeTask::create($task);
            }
        }

        if($amount == 30){
            $task['amount'] = 30;
            $task['sn'] = date('YmdHis') . Str::random(16);
            MobileRechargeTask::create($task);
        }

    }


    // 0待充值，1请求充值成功，2请求充值失败，4充值失败，9充值成功
    public function getStatusNameAttribute()
    {
        if ($this->status == 0) {
            return '待充值';
        }
        if ($this->status == 1) {
            return '请求成功';
        }
        if ($this->status == 2) {
            return '请求失败';
        }
        if ($this->status == 4) {
            return '充值失败';
        }
        if ($this->status == 9) {
            return '充值成功';
        }
    }
}